package com.iye.puretennis.custom;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import com.iye.puretennis.R;
import com.iye.puretennis.helper.Constant;

import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2014/11/18
 * Time: 11:28
 */
public class UploadImagePopupWindow extends PopupWindow {

    public static String FILE_PATH;

    private Button btnTakePhoto;
    private Button btnPickPhoto;


    public UploadImagePopupWindow(final Activity context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mainView = inflater.inflate(R.layout.uploadimage, null);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        View.OnClickListener buttonClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int viewId=view.getId();
                dismiss();
                if(viewId==btnPickPhoto.getId()){
                    Intent intent = new Intent(Intent.ACTION_PICK, null);
                    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Constant.IMAGE_UNSPECIFIED);
                    context.startActivityForResult(intent, Constant.PICK_PHOTO);
                }

                if(viewId==btnTakePhoto.getId()){
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    FILE_PATH = Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + ".png";
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(FILE_PATH)));
                    intent.putExtra("return-data", true);
                    context.startActivityForResult(intent, Constant.TAKE_PHOTO);
                }
            }
        };
        Button btnCancel = (Button) mainView.findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(buttonClickListener);
        btnTakePhoto = (Button) mainView.findViewById(R.id.btn_take_photo);
        btnPickPhoto = (Button) mainView.findViewById(R.id.btn_pick_photo);
        btnTakePhoto.setOnClickListener(buttonClickListener);
        btnPickPhoto.setOnClickListener(buttonClickListener);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setContentView(mainView);
        this.setFocusable(true);
    }

}
